//
//  FlowCell.swift
//  Employee
//
//  Created by swordjoy on 2017/6/26.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

protocol FlowCellDelegate: class {
  func flowCellMore(indexPath: IndexPath, isMore: Bool)
  func flowCellNext(indexPath: IndexPath, location: Int)
}

class FlowCell: UITableViewCell {
  
  static let identifiler = "FlowCell.swordjoy"
  static func height(with isOn: Bool, number: Int) -> CGFloat {
    if isOn { return 70 + 50 * number.cgFloat + 10 }
    else { return 70 }
  }
  
  var model: Flow? {
    didSet {
      guard let temp = model else { return }
      titleLabel.text = temp.name
    }
  }
  
  
  var indexPath: IndexPath!
  
  var isMore: Bool = false {
    didSet {
      moreButton.isSelected = isMore
      for label in labels {
        label.removeFromSuperview()
      }
      labels.removeAll()
      if moreButton.isSelected {
        containerView.backgroundColor = GlobalConfig.BGSkyBlueColor
        titleLabel.textColor = UIColor.white
        guard let temp = model else { return }
        for (index, item) in temp.flowTypeDtos.enumerated() {
          let label = UILabel.getLabel(with: GlobalConfig.system16Font, textColor: UIColor.white, textAlignment: .left)
          label.backgroundColor = UIColor.clear
          label.text = item.name
          containerView.addSubview(label)
          label.snp.makeConstraints({ (make) in
            make.left.equalTo(40)
            make.top.equalTo(60 + index * 50 )
            make.right.equalTo(-40)
            make.height.equalTo(50)
          })
          label.tag = index
          let tap = UITapGestureRecognizer(target: self, action: #selector(subTypeTapAction(_:)))
          label.isUserInteractionEnabled = true
          label.addGestureRecognizer(tap)
          labels.append(label)
        }
      }
      else {
        titleLabel.textColor = GlobalConfig.TextDeepColor
        containerView.backgroundColor = UIColor.white
      }
    }
  }
  
  weak var delegate: FlowCellDelegate?
  
  fileprivate var containerView: UIView!
  fileprivate var titleLabel: UILabel!
  fileprivate var moreButton: UIButton!
  fileprivate var labels: [UILabel] = []
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = GlobalConfig.BGWhiteColor
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

extension FlowCell {

  @objc fileprivate func subTypeTapAction(_ gesture: UITapGestureRecognizer) {
    guard let temp = gesture.view as? UILabel else { return }
    delegate?.flowCellNext(indexPath: indexPath, location: temp.tag)
  }
  
    @objc fileprivate func cellTapAction() {
        moreButton.isSelected = !moreButton.isSelected
        delegate?.flowCellMore(indexPath: indexPath, isMore: moreButton.isSelected)
    }
    
}

// MARK: - fileprivate

extension FlowCell {
  fileprivate func create() {
    containerView = UIView()
    contentView.addSubview(containerView)
    containerView.backgroundColor = UIColor.white
    containerView.snp.makeConstraints { (make) in
      make.left.top.equalTo(10)
      make.right.equalTo(-10)
      make.bottom.equalToSuperview()
    }
    containerView.layer.cornerRadius = 2
    containerView.clipsToBounds = true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellTapAction))
    containerView.addGestureRecognizer(tapGesture)
    
    titleLabel = UILabel.getLabel(with: GlobalConfig.system16Font, textColor: GlobalConfig.TextDeepColor, textAlignment: .left)
    containerView.addSubview(titleLabel)
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(15)
      make.top.equalTo(22)
      make.right.equalTo(100)
    }
    
    moreButton = UIButton()
    containerView.addSubview(moreButton)
    let offImage = UIImage(named: "func_more_off")
    let onImage = UIImage(named: "func_more_on")
    moreButton.setImage(offImage, for: .normal)
    moreButton.setImage(onImage, for: .selected)
    moreButton.isSelected = false
    moreButton.snp.makeConstraints { (make) in
        make.top.equalTo(27)
      make.right.equalTo(-15)
      make.height.width.equalTo(10)
    }
  }
  
}
